﻿<?php
    include("room_image_class.php");
    include_once ("hotel_class.php");
    include ("Elements/Hotel/hotel_details.php");
    include ("Elements/Room/room_type.php");
    include ("Elements/Hotel/hotel_image.php");
/**
 * Description of APIObject
 *
 * @author cristian.daraban
 */
class APIObject {
    private $urlBase="http://api.ean.com/ean-services/rs/hotel/v3/";
    private $urlString="";
    // Common Request Elements as param
    public function APIObject(
            $cid="55505", 
            $apiKey="", 
            $minorRev="16", 
            $locale="en_US", 
            $currencyCode="USD", 
            $customerSessionId="", 
            $customerIpAddress="", 
            $customerUserAgent="", 
            $type="json") {
        
        try{
            if(isset($cid) && $cid != "" ){
                $this->urlString .="cid=".$cid;
            }
            else{                
                return;
            }
            if (isset($apiKey) && $apiKey != "" ) {
                $this->urlString .="&apiKey=".$apiKey;
            }
            if (isset($minorRev) && $minorRev != "" ) {
                $this->urlString .="&minorRev=".$minorRev;
            }
            if (isset($locale) && $locale != "" ) {
                $this->urlString .="&locale=".$locale;
            }
            if (isset($currencyCode) && $currencyCode != "" ) {
                $this->urlString .="&"."currencyCode=".$currencyCode;
            }  
            if (isset($customerSessionId) && $customerSessionId != "" ) {
                $this->urlString .="&customerSessionId=".$customerSessionId;
            }
            if (isset($customerIpAddress) && $customerIpAddress != "" ) {
                $this->urlString .="&customerIpAddress=".$customerIpAddress;
            }
            if (isset($customerUserAgent) && $customerUserAgent != "" ) {
                $this->urlString .="&customerUserAgent=".$customerUserAgent;
            }
            if (isset($type) && $type != "" ) {
                $this->urlString .="&_type=".$type;
            }
        }
        catch (Exception $error_string){
            print_r($error_string);
        }
    }
    
    function GetAPIString() {
        return $this->urlString;
    }
    
    function PrintAPIString() {
        print_r(($this->urlString));
    }
    
    function GetHotelList(
            $arrivalDate="",
            $departureDate="",
            $numberOfResults="",
            $city="",
            $stateProvinceCode="",
            $countryCode="",
            $destinationString="",
            $destinationId="",
            $hotelIdList="",
            $latitude="",
            $longitude="",
            $searchRadius="",
            $searchRadiusUnit="",
            $address="",
            $postalCode="",
            $propertyName="",
            $propertyCategory="",
            $amenities="",
            $maxStarRating="",
            $minStarRating="",
            $minRate="",
            $maxRate="",
            $supplierType="",
            $numberOfBedRooms="",
            $sort="",
            $maxRatePlanCount="",
            $RoomGroup="",
            $options="",
            $supplierCacheTolerance="",
            $cacheLocation="",
            $cacheKey=""
            ) {
        // Concatenate the url with the request type
        // and the basic request elements from the constructor
        // into a single variable @var = $url
        $url = "";
        $url .= $this->urlBase;
        $url .= "list?";
        $url .= $this->urlString;
        if(isset($arrivalDate) || $arrivalDate != ""){
            $url .="&arrivalDate=".$arrivalDate;
        }
        if(isset($departureDate) || $departureDate != ""){
            $url .="&departureDate=".$departureDate;
        }
        if(isset($numberOfResults) || $numberOfResults != ""){
            $url .="&numberOfResults=".$numberOfResults;
        }        
        if(isset($city) || $city != ""){
            $url .="&city=".$city;
        }
        if(isset($stateProvinceCode) || $stateProvinceCode != ""){
            $url .="&stateProvinceCode=".$stateProvinceCode;
        }
        if(isset($countryCode) || $countryCode != ""){
            $url .="&countryCode=".$countryCode;
        }
        if(isset($destinationString) || $destinationString != ""){
            $url .="&destinationString=".$destinationString;
        }
        if(isset($destinationId) || $destinationId != ""){
            $url .="&destinationId=".$destinationId;
        }
        if(isset($hotelIdList) || $hotelIdList != ""){
            $url .="&hotelIdList=".$hotelIdList;
        }
        if(isset($latitude) || $latitude != ""){
            $url .="&latitude=".$latitude;
        }
        if(isset($longitude) || $longitude != ""){
            $url .="&longitude=".$longitude;
        }
        if(isset($searchRadius) || $searchRadius != ""){
            $url .="&searchRadius=".$searchRadius;
        }
        if(isset($searchRadiusUnit) || $searchRadiusUnit != ""){
            $url .="&searchRadiusUnit=".$searchRadiusUnit;
        }
        if(isset($address) || $address != ""){
            $url .="&address=".$address;
        }
        if(isset($postalCode) || $postalCode != ""){
            $url .="&postalCode=".$postalCode;
        }
        if(isset($propertyName) || $propertyName != ""){
            $url .="&propertyName=".$propertyName;
        }
        if(isset($propertyCategory) || $propertyCategory != ""){
            $url .="&propertyCategory=".$propertyCategory;
        }
        if(isset($amenities) || $amenities != ""){
            $url .="&amenities=".$amenities;
        }
        if(isset($maxStarRating) || $maxStarRating != ""){
            $url .="&maxStarRating=".$maxStarRating;
        }
        if(isset($minStarRating) || $minStarRating != ""){
            $url .="&minStarRating=".$minStarRating;
        }
        if(isset($minRate) || $minRate != ""){
            $url .="$minRate=".$minRate;
        }
        if(isset($maxRate) || $maxRate != ""){
            $url .="&maxRate=".$maxRate;
        }
        if(isset($supplierType) || $supplierType != ""){
            $url .="&supplierType=".$supplierType;
        }
        if(isset($numberOfBedRooms) || $numberOfBedRooms != ""){
            $url .="&numberOfBedRooms=".$numberOfBedRooms;
        }
        if(isset($sort) || $sort != ""){
            $url .="&sort=".$sort;
        }
        if(isset($maxRatePlanCount) || $maxRatePlanCount != ""){
            $url .="&maxRatePlanCount=".$maxRatePlanCount;
        }
        if(isset($RoomGroup) || $RoomGroup != ""){
            $url .="&RoomGroup=".$RoomGroup;
        }
        if(isset($options) || $options != ""){
            $url .="&options=".$options;
        }
        if(isset($supplierCacheTolerance) || $supplierCacheTolerance != ""){
            $url .="&supplierCacheTolerance=".$supplierCacheTolerance;
        }
        if(isset($cacheLocation) || $cacheLocation != ""){
            $url .="&cacheLocation=".$cacheLocation;
        }
        if(isset($cacheKey) || $cacheKey != ""){
            $url .="&cacheKey=".$cacheKey;
        }

        // now we contruct the means to send a request with the params
        // gathered so far ...
        // we are using curl.dll from php to send the request
        $header[] = "Accept: application/json";
        $header[] = "Accept-Encoding: gzip";
                
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_ENCODING , "gzip");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // we then decode the string to a more readable json format
        $response = json_decode(curl_exec($ch));
        //print_r(htmlspecialchars($url));
        // we need to parse the json format ... and create
        // a new array of hotel objects which will be returned by the function
        $size=$response->{"HotelListResponse"}->{"HotelList"}->{"@size"};
        for($i=0;$i<$size;$i++)
        {
            $hotelList[$i]=new Hotel($response->{"HotelListResponse"}->{"HotelList"}->{"HotelSummary"}[$i]);
        }
        return $hotelList;
        
    }
    function GetGeoLocation(
            $address="",
            $city="",
            $stateProvinceCode="",
            $countryCode="",
            $postalCode="",
            $destinationString="",
            $destinationId="",
            $ignoreSearchWeight="",
            $useGeoCoder="",
            $type="") {
                // Concatenate the url with the request type
        // and the basic request elements from the constructor
        // into a single variable @var = $url
        $url = "";
        $url .= $this->urlBase;
        $url .= "geoSearch?";
        $url .= $this->urlString;
        
        if(isset($address) || $address != ""){
            $this->urlString .="&address=".$address;
        }
        if(isset($city) || $city != ""){
            $url .="&city=".$city;
        }
        if(isset($stateProvinceCode) || $stateProvinceCode != ""){
            $url .="&stateProvinceCode=".$stateProvinceCode;
        }        
        if(isset($postalCode) || $postalCode != ""){
            $url .="&$postalCode=".$postalCode;
        }
        if(isset($ignoreSearchWeight) || $ignoreSearchWeight != ""){
            $url .="&ignoreSearchWeight=".$ignoreSearchWeight;
        }
        if(isset($countryCode) || $countryCode != ""){
            $url .="&countryCode=".$countryCode;
        }
        if(isset($destinationString) || $destinationString != ""){
            $url .="&destinationString=".$destinationString;
        }
        if(isset($destinationId) || $destinationId != ""){
            $url .="&destinationId=".$destinationId;
        }
        if(isset($useGeoCoder) || $useGeoCoder != ""){
            $url .="&useGeoCoder=".$useGeoCoder;
        }
        if(isset($type) || $type != ""){
            $url .="&type=".$type;
        }
    }
    
    function GetRoomAvaiability(
            $hotelId, 
            $arrivalDate, 
            $departureDate, 
            $numberOfBedRooms, 
            $supplierType, 
            $rateKey,
            $rateCode,
            $roomTypeCode,
            $includeDetails,
            $includeRoomImages,
            $options,
            $RoomGroup) {
        // Concatenate the url with the request type
        // and the basic request elements from the constructor
        // into a single variable @var = $url
        $url = "";
        $url .= $this->urlBase;
        $url .= "avail?";
        $url .= $this->urlString;
        
        if(isset($hotelId) || $hotelId != "")
        {
            $url .="&hotelId=".$hotelId;
        }
        if(isset($arrivalDate) || $arrivalDate != "")
        {
            $url .="&arrivalDate=".$arrivalDate;
        }
        if(isset($departureDate) || $departureDate != "")
        {
            $url .="&departureDate=".$departureDate;
        }
        if(isset($numberOfBedRooms) || $numberOfBedRooms != "")
        {
            $url .="&numberOfBedRooms=".$numberOfBedRooms;
        }
        if(isset($supplierType) || $supplierType != "")
        {
            $url .="&supplierType=".$supplierType;
        }
        if(isset($rateKey) || $rateKey != "")
        {
            $url .="&rateKey=".$rateKey;
        }
        if(isset($rateCode) || $rateCode != "")
        {
            $url .="&rateCode=".$rateCode;
        }
        if(isset($roomTypeCode) || $roomTypeCode != "")
        {
            $url .="&roomTypeCode=".$roomTypeCode;
        }
        if(isset($includeDetails) || $includeDetails != "")
        {
            $url .="&includeDetails=".$includeDetails;
        }
        if(isset($includeRoomImages) || $includeRoomImages != "")
        {
            $url .="&includeRoomImages=".$includeRoomImages;
        }
        if(isset($options) || $options != "")
        {
            $url .="&options=".$options;
        }
        if(isset($RoomGroup) || $RoomGroup != "")
        {
            $url .="&RoomGroup=".$RoomGroup;
        }
        
        $header[] = "Accept: application/json";
        $header[] = "Accept-Encoding: gzip";
  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_ENCODING , "gzip");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $response = json_decode(curl_exec($ch));
        //print_r($response);
        return $response;
        
        
    }
    
  /*  private function GetRoomGroupXML() {
        
    }*/
    
    
    
    
    /*  
     * Retrieve a list of room images for their respective roomTypeCodes 
     * with a specific hotelId
     * 
     * HotelRoomImageResponse Elements:
     *      RoomImages:(array of RoomImage)
     * RoomImage Response Elements:
     *      roomTypeCode:(string)	
     *          Room type code for the room depicted
     *      url:(string)
     *          URL for room image
     */
    function GetRoomImages($hotelId){
        // Concatenate the url with the request type
        // and the basic request elements from the constructor
        // into a single variable @var = $url
        $url = "";
        $url .= $this->urlBase;
        $url .= "roomImages?";
        $url .= $this->urlString;
        if(isset($hotelId) || $hotelId != "")
        {
            $url .="&hotelId=".$hotelId;
        }


        $header[] = "Accept: application/json";
        $header[] = "Accept-Encoding: gzip";
  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_ENCODING , "gzip");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $response = json_decode(curl_exec($ch));
        //print_r($response);
        $size=$response->{"HotelRoomImageResponse"}->{"RoomImages"}->{"@size"};
	for($i=0;$i<$size;$i++)
	{
            $roomImages[$i]=new RoomImage($response->{"HotelRoomImageResponse"}->
                    {"RoomImages"}->{"RoomImage"}[$i]);
	}
	return $roomImages;
    }
    
    function GetHotelInformation($hotelId, $options="DEFAULT"){
        
        // Concatenate the url with the request type
        // and the basic request elements from the constructor
        // into a single variable @var = $url
        $url = "";
        $url .= $this->urlBase;
        $url .= "info?";
        $url .= $this->urlString;
        
        /* checking for empty params */
        !empty($hotelId) ? $url .="&hotelId=".$hotelId : true;
        
        /* Using curl for sending the request and getting the response */
        $header[] = "Accept: application/json";
        $header[] = "Accept-Encoding: gzip";
                
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_ENCODING , "gzip");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $response = json_decode(curl_exec($ch));
        
     
        $hotelInfoResp = $response->{"HotelInformationResponse"};
        
        //print_r($hotelInfoResp);
        
        switch($options)
        {
            default:
                return ;
                break;
            case "HOTEL_SUMMARY":
                return;
                break;
            case "HOTEL_DETAILS":
                return (new HotelDetails($hotelInfoResp->{"HotelDetails"}));
                break;
            case "SUPPLIERS":
                return;
                break;
            case "ROOM_TYPES":
                $size = $hotelInfoResp->{"RoomTypes"}->{"@size"};
                $roomTypes = array();
                for($i=0; $i< $size; $i++)
                {
                    $rooms = $hotelInfoResp->{"RoomTypes"};
                    //print_r($rooms);
                    $roomTypes[$i] = new RoomType($rooms->{"RoomType"}[$i]);
                }
                return $roomTypes;
                break;
            case "ROOM_AMENITIES":
                return;
                break;
            case "PROPERTY_AMENITIES":
                return;
                break;
            case "HOTEL_IMAGES":
                $size = $hotelInfoResp->{"HotelImages"}->{"@size"};
                $hotelImages = array();
                for($i=0; $i< $size; $i++)
                {
                    $images = $hotelInfoResp->{"HotelImages"};
                    //print_r($images->{"HotelImage"}[$i]);
                    $hotelImages[$i] = new HotelImage($images->{"HotelImage"}[$i]);
                }
                return $hotelImages;
                break;     
        }
        
    }

    
}

?>
